<style lang="scss" scoped>
    .home{
        height: calc(100vh - 55px);
        overflow-y: auto;
        background-color: #f5f5f5;
        padding-top: 50px;
        .navigator{
            width: 100%;
            box-sizing: border-box;
            height: 50px;
            padding: 5px 4%;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 999;
            display: flex;
            background-color: rgb(50, 134, 255);
            .headImg{
                width: 50px;
                img{
                    width: 40px;
                    height: 40px;
                    border-radius: 50%;
                }
            }
            .search{
                width: calc(100% - 130px);
                line-height: 40px;
                height: 40px;
                input{
                    border: 1px solid #eee;
                    border-radius: 20px;
                    outline: none;
                    width: 100%;
                    padding-left: 10px;
                    background-color: rgba(255,255,255,0.1);
                    &::-webkit-input-placeholder{
                        color: #eee;
                    }
                }
            }
            .btn{
                width: 80px;
                text-align: right;
                button{
                    line-height: 40px;
                    background-color: rgba(0,0,0,0);
                    border: 0;
                    color: #fff;
                    &:active{
                        color: #333;
                    }
                }
            }
        }
        .swiper{
            img{
                width: 100%;
                height: 200px;
            }
        }
        .catgory{
            width: 90%;
            margin-left: 5%;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 7px;
            padding: 5px 10px;
            // height: 120px;
            margin-top: 15px;
            display: flex;
            flex-wrap: wrap;
            .catItem{
                width: 25%;
                text-align: center;
                img{
                    height: 100px;
                }
            }
        }
        .product{
            width: 90%;
            margin-left: 5%;
            margin-top: 15px;
            display: flex;
            justify-content: space-between;
            .proLeft,
            .proRight{
                width: 48%;
                .proList{
                    background-color: #fff;
                    border-radius: 7px;
                    overflow: hidden;
                    margin-bottom: 20px;
                    img{
                        width: 100%;
                    }
                    .proName{
                        height: 45px;
                        box-sizing: border-box;
                        padding: 5px 7px;
                        line-height: 20px;
                        // 省略号
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                    .pri{
                        height: 35px;
                        box-sizing: border-box;
                        padding: 3px 7px;
                        line-height: 29px;
                        display: flex;
                        justify-content: space-between;
                        .nP{
                            color: #ff0000;
                        }
                        span{
                            font-size: 12px;
                            font-weight: bold;
                        }
                        .oP{
                            font-size: 14px;
                            text-decoration: line-through;
                            color: #999;
                        }
                    }
                    .number{
                        padding: 0 7px;
                        height: 30%;
                        line-height: 35px;
                        display: flex;
                        justify-content: space-between;
                        font-size: 14px;
                        color: #999;
                    }
                }
            }
        }
    }
</style>
<template>
    <div class="home">
        <div class="navigator">
            <div class="headImg">
                <img :src="$store.state.userInfo.headImg === null ? '' : $store.state.userInfo.headImg" alt="">
            </div>
            <div class="search" @click="$router.push('/search')">
                <input type="text" placeholder="搜索" readonly>
            </div>
            <div class="btn">
                <button>
                    搜索
                </button>
            </div>
        </div>
        <div class="swiper">
            <van-swipe :autoplay="3000">
                <van-swipe-item v-for="(image, index) in result.banner_list" :key="index">
                    <img v-lazy="image" />
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="catgory">
            <div class="catItem" v-for="item in result.categary_list" :key="item.id">
                <img :src="item.url.replace('192.168.17.126','192.168.13.10').replace(/'/g,'')" alt="">
            </div>
        </div>
        <div class="product">
            <div class="proLeft">
                <div v-for="(item,index) in result.product_list" :key="item.ID">
                    <div class="proList" 
                        v-if="index % 2 != 0"
                        @click="$router.push(`/product_detail?product_id=${item.ID}&admin_id=${item.admin_id}`)"
                        >
                        <div class="proImg">
                            <img :src="item.duck_img" alt="">
                        </div>
                        <div class="proName">
                            {{ item.duck_name }}
                        </div>
                        <div class="pri">
                            <div class="nP">
                                <span>
                                    ￥
                                </span>
                                {{ item.duck_min_price }}
                            </div>
                            <div class="oP">
                                ￥{{ item.duck_no_price }}
                            </div>
                        </div>
                        <div class="number">
                            <span>
                                销量:{{ 
                                        item.duck_hot > 999 ? '999+': item.duck_hot
                                    }}
                            </span>
                            <span>
                                库存: {{ 
                                        item.num > 999 ? '999+' : item.num
                                    }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="proRight">
                <div v-for="(item,index) in result.product_list" :key="item.ID">
                    <div class="proList" 
                        v-if="index % 2 == 0"
                        @click="$router.push(`/product_detail?product_id=${item.ID}&admin_id=${item.admin_id}`)"
                        >
                        <div class="proImg">
                            <img :src="item.duck_img" alt="">
                        </div>
                        <div class="proName">
                            {{ item.duck_name }}
                        </div>
                        <div class="pri">
                            <div class="nP">
                                <span>
                                    ￥
                                </span>
                                {{ item.duck_min_price }}
                            </div>
                            <div class="oP">
                                ￥{{ item.duck_no_price }}
                            </div>
                        </div>
                        <div class="number">
                            <span>
                                销量:{{ 
                                        item.duck_hot > 999 ? '999+': item.duck_hot
                                    }}
                            </span>
                            <span>
                                库存: {{ 
                                        item.num > 999 ? '999+' : item.num
                                    }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>

// 引入首页接口
import {getHome} from '@/api'
    export default {
        methods: {
            getList(){
                getHome({admin_id:125}).then(res => {
                    if(res.data.code == 1){
                        this.result.categary_list = res.data.result.categary_list;
                        this.result.product_list = JSON.parse(
                            // 商品的JSON数据压缩成JSON字符串 进行字符串的替换
                            JSON.stringify(res.data.result.product_list)
                            .replace(/www.poralmall.com/g,'192.168.13.10')
                            .replace(/192.168.17.126/g,'192.168.13.10')
                        )
                        res.data.result.banner_list.forEach(item => {
                            this.result.banner_list = [
                                ...this.result.banner_list,
                                ...item.url.replace(/192.168.17.126/g,'192.168.13.10').replace("'[","").replace("]'","").replace(/["]+/g,"").split(',')
                            ]
                            // 相当于 Array.concat(arr,arr1)
                        });
                        // console.log(this.result.product_list);
                    }else{
                        this.$toast(
                            JSON.stringify(res.data)
                        )
                    }
                })
            }
        },
        data() {
            return {
                // headImg: null,
                result: {
                    banner_list: [],
                    categary_list: [],
                    product_list: []
                }
            }
        },
        created(){
            this.getList();
            // if(this.$store.state.userInfo.ID){
            //     this.headImg = this.$store.state.userInfo.headImg;
            // }
            // try{
            //     // 尝试转换 JSON格式
            //     // this.headImg = JSON.parse(localStorage.getItem('userInfo')).headImg;
                

            // }catch(e){
            //     // 转换失败进入catch
            //     console.log('还未登录');
            // }
        }
    }
</script>